﻿var SearchModel = {
    hoTen: ko.observable(""),
    tenDonVi: ko.observable(""),
    tenCongTrinh: ko.observable(""),
    linhVucId: ko.observable(0),
    hocHamId: ko.observable(0),
    hocViId: ko.observable(0)
};

function SearchViewModel() {
    var self = this;

    // data
    self.searchModel = ko.observable(SearchModel);

    // linh vuc
    self.linhVucs = ko.observableArray();
    $.blockUI();
    $.ajax({
        url: "Default.aspx/LinhVucs",
        type: "post",
        contentType: "application/json",
        async: false,
        success: function (data) {
            self.linhVucs(data.d);
        }
    });

    // hoc ham
    self.hocHams = ko.observableArray();
    $.ajax({
        url: "Default.aspx/HocHams",
        type: "post",
        contentType: "application/json",
        async: false,
        success: function (data) {
            self.hocHams(data.d);
        }
    });

    // hoc vi
    self.hocVis = ko.observableArray();
    $.ajax({
        url: "Default.aspx/HocVis",
        type: "post",
        contentType: "application/json",
        async: false,
        success: function (data) {
            self.hocVis(data.d);
        }
    });

    // load from cookie
    if ($.cookie('ol_hoTen')) self.searchModel().hoTen($.cookie('ol_hoTen'));
    if ($.cookie('ol_tenDonVi')) self.searchModel().tenDonVi($.cookie('ol_tenDonVi'));
    if ($.cookie('ol_tenCongTrinh')) self.searchModel().tenCongTrinh($.cookie('ol_tenCongTrinh'));
    if ($.cookie('ol_linhVucId')) self.searchModel().linhVucId($.cookie('ol_linhVucId'));
    if ($.cookie('ol_hocHamId')) self.searchModel().hocHamId($.cookie('ol_hocHamId'));
    if ($.cookie('ol_hocViId')) self.searchModel().hocViId($.cookie('ol_hocViId'));

    // events
    self.search = function () {
        // save cookie
        $.cookie('ol_hoTen', self.searchModel().hoTen(), { expires: 365 });
        $.cookie('ol_tenDonVi', self.searchModel().tenDonVi(), { expires: 365 });
        $.cookie('ol_tenCongTrinh', self.searchModel().tenCongTrinh(), { expires: 365 });
        $.cookie('ol_linhVucId', self.searchModel().linhVucId(), { expires: 365 });
        $.cookie('ol_hocHamId', self.searchModel().hocHamId(), { expires: 365 });
        $.cookie('ol_hocViId', self.searchModel().hocViId(), { expires: 365 });

        $('#ListContainer').jtable('load', { searchModel: ko.toJS(self.searchModel) });
    };

    self.reset = function() {
        self.searchModel().hoTen("");
        self.searchModel().tenDonVi("");
        self.searchModel().tenCongTrinh("");
        self.searchModel().linhVucId(0);
        self.searchModel().hocHamId(0);
        self.searchModel().hocViId(0);

        //remove cookies
        $.removeCookie('ol_hoTen');
        $.removeCookie('ol_tenDonVi');
        $.removeCookie('ol_tenCongTrinh');
        $.removeCookie('ol_linhVucId');
        $.removeCookie('ol_hocHamId');
        $.removeCookie('ol_hocViId');

        self.search();
    };

    self.search();
}